home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
HPAVC
/
HPAVC CD-ROM.iso
/
ARASAN_S.ZIP
/
LOG.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-06
|
5KB
|
226 lines
// Copyright 1994 by Jon Dart. All Rights Reserved.
#include <stddef.h>
#include <string.h>
#include <windows.h>
#include <assert.h>
#include "log.h"
#include "notation.h"
#define LOG_FILE_NAME "ARASAN.LOG"
#define MAX_ANAL 5
Log_Entry::Log_Entry(const ReversibleMove &move,
const char *move_image)
: my_move(move),
my_image(new char[strlen(move_image)+1])
{
strcpy(my_image,move_image);
}
Log_Entry::Log_Entry()
: my_move(ReversibleMove()),my_image(NULL)
{
}
Log_Entry::Log_Entry( const Log_Entry &le)
{
my_move = le.my_move;
my_image = new char[strlen(le.my_image)+1];
assert(my_image);
strcpy(my_image,le.my_image);
}
Log_Entry & Log_Entry::operator = (const Log_Entry &le)
{
if (&le != this)
{
my_move = le.my_move;
delete [] my_image;
my_image = new char[strlen(le.my_image)+1];
assert(my_image);
strcpy(my_image,le.my_image);
}
return *this;
}
Log_Entry::~Log_Entry()
{
delete [] my_image;
}
static char *time_image(const time_t time)
{
static char buf[80];
int hrs = time / 3600;
int mins = (time - (hrs*3600))/60;
int secs = time - (hrs*3600) - (mins*60);
wsprintf(buf,"%02d:%02d:%02d",hrs,mins,secs);
return buf;
}
Log::Log() : Array<Log_Entry>(Initial_Log_Size,False)
{
my_current = 0;
*buf = '\0';
log_file.open((LPSTR)LOG_FILE_NAME,ios::out|ios::trunc);
if (!log_file.good())
{
MessageBox(NULL,"Can't open log file. Game moves will not be saved.","",MB_OK);
}
}
Log::~Log()
{
if (log_file.good())
{
log_file << buf << endl;
log_file.close();
}
}
void Log::add_move( Board &board, const ReversibleMove &emove,
const char *move_image, const Search::Statistics *stats,
const Boolean toFile )
{
Log_Entry entry( emove, move_image );
// moves are always added at the "current" position.
resize(current());
*this += entry;
my_current++;
if (!toFile)
return;
char num[10];
*buf = '\0';
wsprintf(num,"%d. ",(num_moves()-1)/2 + 1);
strcat(buf,num);
strcat(buf,move_image);
int len = strlen(buf);
char *p = buf + len;
for (int i = 0; i < 15-len; i++)
*p++ = ' ';
*p = '\0';
if (stats)
{
char statbuf[80];
if (stats->num_moves == 0)
strcpy(statbuf," (book)");
else
{
char pred[50];
*pred = '\0';
Board tmp_board = board;
tmp_board.MakeMove(emove);
for (int i=1; i<=MAX_ANAL; i++)
{
Move m = stats->best_line[i];
if (m.IsNull())
break;
char pred_move_image[20];
*pred_move_image = '\0';
Notation::Image(tmp_board,m,pred_move_image);
strcat(pred,pred_move_image);
strcat(pred," ");
tmp_board.MakeMove(ExtendedMove(tmp_board,m));
}
wsprintf(statbuf,"\t%s %d\t%ld\t%ld\t%5d\t%s",
time_image(stats->elapsed_time),
stats->plies_completed,
stats->num_moves,
stats->num_pos,
stats->value,pred);
}
if (stats->state == Search::Checkmate)
strcat(statbuf," mate");
else if (stats->state == Search::Stalemate)
strcat(statbuf," stalemate");
else if (stats->state == Search::Resigns)
strcat(statbuf," resigns");
else if (stats->state == Search::Draw)
strcat(statbuf," draw");
strcat(buf,statbuf);
}
log_file << buf << endl;
*buf = '\0';
}
const ReversibleMove &Log::move( const unsigned n ) const
{
return (*this)[n].move();
}
const ReversibleMove &Log::last_move() const
{
assert (current());
return move(current()-1);
}
void Log::remove_move()
{
if (current() == 0)
return;
resize(current()-1);
--my_current;
}
Boolean Log::back_up()
{
if (my_current >0)
{
--my_current;
return True;
}
else
return False;
}
Boolean Log::go_forward()
{
if (my_current < size())
{
++my_current;
return True;
}
else
return False;
}
void Log::reset()
{
my_current = 0;
}
void Log::clear()
{
write_eol();
resize(0);
my_current = 0;
}
void Log::write_header()
{
flush();
static char header[] = " move time depth\tmoves\tnodes\tvalue\tpredicted";
log_file << header << endl;
}
void Log::write(char *s)
{
flush();
log_file << s;
}
void Log::write_eol()
{
log_file << endl;
}
void Log::flush()
{
if (strlen(buf) > 0)
{
log_file << buf << endl;
}
*buf = '\0';
}